﻿#include "VertexShader.h"
#include "Graphics.h"
#include "Misc.h"
#include "WinFunc.h"

namespace Base {

	VertexShader::VertexShader(const char* relativePath,
		const char* entry) {
		this->Init(relativePath, entry);
	}

	void VertexShader::Init(const char* relativePath,
		const char* entry) {
		auto graphics = Graphics::GetInstance();
		auto device = graphics->GetDevice();

		// 创建 vertexShader
		DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
		shaderFlags |= D3DCOMPILE_DEBUG;
#endif

		// create vertex shader
		ComPtr<ID3DBlob> errorBuffer;
		D_HRESULT(D3DCompileFromFile(
			WinFunc::StrToWstr(relativePath).c_str(),
			nullptr,	// pDefines
			nullptr,    // pInclude
			entry,  // pEntrypoint
			"vs_4_0",   // pTarget
			shaderFlags, // Flags1
			0,			 // Flags2
			&m_vsBuffer,   // ppCode
			&errorBuffer // ppErrorMsgs
		));
		if (errorBuffer != nullptr) {
			auto msg = reinterpret_cast<char*>(errorBuffer->GetBufferPointer());
			Debug::ThrowOrMessageBox(msg);
		}
		D_HRESULT(device->CreateVertexShader(
			m_vsBuffer->GetBufferPointer(),
			m_vsBuffer->GetBufferSize(),
			0,
			&m_vertexShader
		));
	}
}